<?php
//add tiku
function addtiku()
{
    global $db,$teacher_id;
    $tid = fs_get_value('tid');
    if(isset($_POST['submit']))
    {
        $tiku_name = fs_get_value('question_db_name');
        $tag =$_POST['dbtag'];
        if($tid=='')
        {       
            if($tiku_name!='')
            {
                   $tiku_query = $db->query("insert into ".DBPREFIX."tiku (`t_name`,`t_createtime`,`t_createuid`,`t_tag`) values ('".$tiku_name."',".time().",".$teacher_id.",'".$tag."')"); 
                   if($tiku_query)
                   {
                           fs_jump2('成功创建题库,是否继续创建?','?do=add_q','?do=tikulist');
                   }
                   else
                   {
                           fs_showmsg('创建题库失败!');
                   }
            }
            else
            {
                    fs_showmsg('题库标题不能为空');
            }
        }
        else{
            if($tiku_name!='')
            {
                   $tiku_query = $db->query("update ".DBPREFIX."tiku set t_name='".$tiku_name."',t_tag='".$tag."' where t_id=".$tid); 
                   if($tiku_query)
                   {
                           fs_showmsg('编辑成功');
                   }
                   else
                   {
                           fs_showmsg('编辑失败!');
                   }
            }
            else
            {
                    fs_showmsg('题库标题不能为空');
            }
        }
    }
}
//insert textarea by ajax
function showInputBox()
{//暂时不用
    $type_id = $_POST['type'];
    $str ='';
    for($i=0;$i<intval($type_id);$i++)
    {
            $script .= "<script type='text/javascript'>CKEDITOR.replace( 'answer".($i+1)."',{ height : '100px', width:'600px',toolbar:'MyToolbar',fontSize_sizes:'16/16px' } );</script>";
            $str .='<tr>
                    <td align="right">答案列表:'.($i+1).'</td>
                    <td><textarea  id="answer'.($i+1).'"></textarea></td>
                    <td width="13%"><input type="checkbox"  name="rightanswer[]" />正确答案</td>
            </tr>';
    }
    $str = $script.$str;
    if($str!='')
    {
            die($str);
    }
}
//submit add  a question
function doadd_q()
{
    global $db,$teacher_id;
    if(isset($_POST['submit']))
    {
        if($_POST['qid']==''){
            $question_db = intval($_POST['question_db']);
            $question_type = intval($_POST['question_type']);
            $q_title = $_POST['q_title'];
            $option = $_POST['answer'];
            $answer = $_POST['rightanswer'];
            $is_easy = intval($_POST['is_easy']);
            $score = intval($_POST['score']);
            $jiexi = $_POST['fenxi'];
            if($question_db!=''&&$question_type!=''&&$q_title!=''&&$option!=''&&$answer!='')
            {
                    $filter_option_array = array_filter($option);
                    $filter_answer_array = array_filter($answer);
                    $option_str = implode('|', $filter_option_array);
                    $answer_str = implode(',', $filter_answer_array); 

                    $query="insert into ".DBPREFIX."question (`q_title`,`q_option`,`q_jiexi`,`q_answer`,`q_score`,`q_diff`,`q_type`,`q_tiku`,`q_createuid`)"
                            ." values('".$q_title."','".$option_str."','".$jiexi."','".$answer_str."',".$score.",".$is_easy.",".$question_type.",".$question_db.",".$teacher_id.")";
                    //print_r($query);exit();
                    $result =$db->query($query);
                    if($result)
                    {
                        fs_jump2('添加成功,是否继续添加?','?do=addquestion','?do=qlist');
                    }
                    else
                    {
                            fs_showmsg('提交失败!');
                    }
             }
            else
            {
                    fs_showmsg('提交失败,请检查题目的完整性!'); 
            }
        }
        else{
            $question_db = intval($_POST['question_db']);
            $question_type = intval($_POST['question_type']);
            $q_title = $_POST['q_title'];
            $option = $_POST['answer'];
            $answer = $_POST['rightanswer'];
            $is_easy = intval($_POST['is_easy']);
            $score = intval($_POST['score']);
            $jiexi = $_POST['fenxi'];
            if($question_db!=''&&$question_type!=''&&$q_title!=''&&$option!=''&&$answer!='')
            {
                    $filter_option_array = array_filter($option);
                    $filter_answer_array = array_filter($answer);
                    $option_str = implode('|', $filter_option_array);
                    $answer_str = implode(',', $filter_answer_array); 
                    $query="update ".DBPREFIX."question set q_title='".$q_title."',q_option='".$option_str."',q_jiexi='".$jiexi."',q_answer='".$answer_str."',q_score=".$score.",q_diff=".$is_easy.",q_type=".$question_type.",q_tiku=".$question_db.",q_createuid=".$teacher_id." where q_id=".$_POST['qid'];
                    $result =$db->query($query);
                    if($result)
                    {
                            fs_showmsg('修改成功','?do=qlist');
                    }
                    else
                    {
                            fs_showmsg('修改失败!');
                    }
             }
            else
            {
                    fs_showmsg('提交失败,请检查题目的完整性!'); 
            }
        }

    }
}
function add_exam()
{//add a exam 
    global $teacher_id,$db;
    if(isset($_POST['submit']))
    {
            if(!isset($_POST['is_p']))
            {
                $exam_title = $_POST['exam_name'];
                //$exam_time = intval($_POST['exam_time']);
                $question_type = array_filter($_POST['question_type']);
                $question_des = array_filter($_POST['question_des']);
                $question_sort = array_filter($_POST['sort']);
                $is_exercise = $_POST['exercise'];
                $query = "insert into ".DBPREFIX."exam (`exam_name`,`exam_pubtime`,`exam_pubuid`,`is_exercise`) values ('".$exam_title."',".time().",".$teacher_id.",".$is_exercise.")";
                $result = $db->query($query);
            }
            else
            {
                $exam_title = $_POST['exam_name'];
                $exam_time = intval($_POST['exam_time']);
                $exam_score = intval($_POST['exam_score']);
                $exam_do = intval($_POST['exam_do']);
                $perscore = intval($_POST['perscore']);
                $question_type = array_filter($_POST['question_type']);
                $question_des = array_filter($_POST['question_des']);
                $question_sort = array_filter($_POST['sort']);
                $query = "insert into ".DBPREFIX."exam (`exam_name`,`exam_time`,`exam_pubtime`,`exam_pubuid`,`exam_score`,`is_do`,`type`) values ('".$exam_title."',".$exam_time.",".time().",".$teacher_id.",".$exam_score.",".$exam_do.",1)";
                $result = $db->query($query);
            }
            if(!$result)
            {
                fs_showmsg('创建试卷失败!');    
            }
            else{
                    $insert_id =$db->insert_id();
            }
                   
            if(!empty($question_type)&&!empty($question_des)&&!empty($question_sort))
            {
                    for($i=0;$i<count(array_filter(&$question_type));$i++)
                    {
                        if(isset($_POST['is_p']))
                        {
                            $thread_query = "insert into ".DBPREFIX."exam_thread (`exam_id`,`thread_type`,`thread_des`,`thread_sort`,`perscore`) value(".$insert_id.",".$question_type[$i].",'".$question_des[$i]."',".$question_sort[$i].",".$perscore.")";
                        }
                        else
                        {
                            $thread_query = "insert into ".DBPREFIX."exam_thread (`exam_id`,`thread_type`,`thread_des`,`thread_sort`) value(".$insert_id.",".$question_type[$i].",'".$question_des[$i]."',".$question_sort[$i].")";

                        }
                            $thread_result = $db->query($thread_query);
                    }
                    if(!$thread_result)
                    {
                         fs_showmsg('创建题型失败!');   
                    }
                    else
                    {
                        if(isset($exam_do))
                            fs_jump2('添加成功,是否继续添加?','?do=add_e&t=e','?do=elist&t=e');
                        else
                            fs_jump2('添加成功,是否继续添加?','?do=add_e&t=p','?do=elist&t=p');
                    }
            }
            else{
                fs_showmsg('试题类型/试题描述/试题排序 都不能为空!'); 
            }       
    }

}
function showquestions(){
    global $db;
    $fromdb = fs_get_value('fromdb');
    $kownledge =fs_get_value('kownledge');
    $threadid = fs_get_value('threadid');
    $list_query ="select * from  ".DBPREFIX."examconnect where thread_id=".$threadid;
    $list_result  = $db->query($list_query);
    $qlist = array();
    while($list_row = $db->fetch_array($list_result))
    {
        $qlist[] = $list_row['q_id'];
    }
    //exit(print_r($qlist));
    if(count($qlist)==1)
    {
        $list = $qlist[0];
    }
    else
    {
        $list = implode(',',$qlist);
    }
    
    if(empty($fromdb))
    {
        fs_showmsg('必须要筛选试题库');
    }
    if($kownledge!=''&&$list='')
    {
        $query = "select * from ".DBPREFIX."question where q_tiku=".$fromdb." and kownledge like '%".$kownledge."%' and q_type=".$_GET['threadtype'];
    }
    elseif($kownledge!=''&&$list!=''){
        $query = "select * from ".DBPREFIX."question where q_tiku=".$fromdb." and kownledge like '%".$kownledge."%' and q_type=".$_GET['threadtype']." and q_id not in(".$list.")";
    }
    elseif($kownledge==''&&$list!='')
    {
        $query = "select * from ".DBPREFIX."question where q_tiku=".$fromdb." and q_type=".$_GET['threadtype']." and  q_id not in(".$list.")";

    }
    else{
        $query = "select * from ".DBPREFIX."question where q_tiku=".$fromdb." and q_type=".$_GET['threadtype'];
    }
    $result = $db->query($query);
    $num = mysql_num_rows($result);
    if($num==0)
    {
        //fs_showmsg('没有找到相关试题!');
        echo "<Script>alert('没有找到相关试题!')</Script>";
        return false;
    }
    while($row = $db->fetch_array($result))
    {
       $question_list .= ' <tr>
                    <td width="5%"><input type="checkbox" name="is_select[]" value="'.$row['q_id'].'"/></td>
                    <td><a href="" target="_blank">'.$row['q_title'].'</a></td>
            </tr>';
    }
    die($question_list);
}
function insertquestion(){
    global $db;
    $questions = $_POST['is_select'];
    $threadid = $_POST['threadid'];
    if($_POST['submit'])
    {
        if(!empty($questions)&&!empty($threadid))
        {
            for($i=0;$i<count($questions);$i++)
            {
                $query = "insert into ".DBPREFIX."examconnect (`thread_id`,`q_id`) values (".$threadid.",".$questions[$i].")";
                $result = $db->query($query);
                $update_query = "update ".DBPREFIX."exam_thread set thread_question_num=thread_question_num+1 where thread_id=".$threadid;
                $db->query($update_query);
            }
            if($result){
                
                //fs_jump('提交成功,是否继续提交?','?do=addthreadquestion');
                fs_showmsg('添加成功!');
            }
        }
        else{
            fs_showmsg('提交失败!');
        }
    }
}
?>
